// 导入表单验证插件
import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [
      // { url: '/repair_pkg/static/uploads/attachment.jpg' },
      // { url: '/repair_pkg/static/uploads/attachment.jpg' },
    ],
    houseId: '', //房屋id
    mobile: '', //手机号
    houseInfo: '', //房屋名称
    repairItemId: '', //维修项目ID
    repairItemName: '', //维修项目名称
    appointment: '', //预约日期
    description: '', //情况描述
  },
  rules: {
    houseId: [{ required: true, message: '请选择报修房屋!' }],
    repairItemId: [{ required: true, message: '请选择维修的项目!' }],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!' },
    ],
    appointment: [{ required: true, message: '请选择预约日期!' }],
    description: [{ required: true, message: '请填写问题描述!' }],
  },
  onLoad({ id }) {
    this.getHouseList()
    this.getRepairList()
    console.log(id)
    if (id) {
      this.getRepairInfo(id)
      wx.setNavigationBarTiltle({
        title: '修改报修信息',
      })
    }
  },
  async getRepairInfo(id) {
    let res = await wx.http.get('/repair/' + id)
    this.setData({ ...res })
  },
  // 选择了日期
  selectDateInfo(event) {
    console.log(event)
    this.setData({
      appointment: wx.utils.dataFormat(event.detail),
      dateLayerVisible: false,
    })
  },
  // 选择图片上传
  afterRead(event) {
    console.log(event.detail)
    const { file } = event.detail
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: wx.http.baseURL + '/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: `Bearer ${getApp().token}`,
      },
      success: (res) => {
        console.log(res)
        const result = res.data
        const { code, message, data } = JSON.parse(result)
        if (code !== 10000) return wx.utils.toast(message)
        this.setData({
          attachment: [...this.data.attachment, { url: data.url }],
        })
      },
      fail: (err) => {
        console.log(err)
      },
    })
  },
  // 删除图片
  deleteImg(event) {
    console.log(event.detail.index)
    this.setData({
      attachment: this.data.attachment.filter((item, index) => {
        return index !== event.detail.index
      }),
    })
  },

  // 获取我的房屋信息
  async getHouseList() {
    let houseList = await wx.http.get('/room')
    houseList = houseList.map((item) => ({
      name: item.point + item.building + item.room,
      id: item.id,
    }))
    this.setData({ houseList })
  },
  // 获取维修项目列表
  async getRepairList() {
    let repairItem = await wx.http.get('/repairItem')
    this.setData({ repairItem })
  },
  // 选择房屋
  selectHouse({ detail }) {
    this.setData({ houseId: detail.id, houseInfo: detail.name })
  },
  // 选择维修项目
  selectRepair({ detail }) {
    this.setData({ repairItemId: detail.id, repairItemName: detail.name })
  },
  // 打开选择房屋
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  // 关闭选择房屋
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // 打开选择维修项目
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  // 关团维修项日
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  // 打开弹出层
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  // 关团弹出层
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  async submitOK() {
    const isPass = this.validate()
    if (!isPass) return
    const { id, houseId, mobile, repairItemId, repairItemName, appointment, description, attachment } = this.data
    await wx.http.post('/repair', {
      id,
      houseId,
      mobile,
      repairItemId,
      repairItemName,
      appointment,
      description,
      attachment,
    })
    wx.utils.toast('操作成功')
    setTimeout(() => {
      this.goList()
    }, 300)
  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
